package ar.edu.unq.tpi.uis.tepe3.wicket.panels;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.apache.wicket.Page;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

public class DefaultCreateNewMenuPanel extends Panel{

	public DefaultCreateNewMenuPanel(String id, IModel model) {
		super(id, model);
		Link editLink = new Link("createNew", model) {
			
			@Override
			public void onClick() {
				String className = "ar.edu.unq.tpi.uis.tepe3.wicket.pages." + getModelObject().getClass().getSimpleName() + "FormPage";
				
				Class<?> targetClass;
				try {
					targetClass = Class.forName(className);
					Constructor<?> constructor = targetClass.getConstructor();
					Page aPage = (Page) constructor.newInstance();
					this.setResponsePage(aPage);
				} catch (ClassNotFoundException e) {
					warn("Ups!:" + e.getMessage());
				} catch (SecurityException e) {
					warn("Ups!:" + e.getMessage());
				} catch (NoSuchMethodException e) {
					warn("Ups!:" + e.getMessage());
				} catch (IllegalArgumentException e) {
					warn("Ups!:" + e.getMessage());
				} catch (InstantiationException e) {
					warn("Ups!:" + e.getMessage());
				} catch (IllegalAccessException e) {
					warn("Ups!:" + e.getMessage());
				} catch (InvocationTargetException e) {
					warn("Ups!:" + e.getMessage());
				}
				
			}
			
		};
		
		add(editLink);
		
	}


}
